import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import PropTypes from 'prop-types'
import zhCN from 'antd/locale/zh_CN'

import { useRequest } from 'alova/client'
import { ConfigProvider, App, } from 'antd'
import { lazy, Suspense, useState } from 'react'
import { useMount, useSessionStorageState } from 'ahooks'
import { BrowserRouter, useRoutes } from 'react-router-dom'

import { Http } from '../API'
import { IndexedDBFun, AddIndexdDB } from '../Utils/IndexedDB'
import { judgeBrowserVersion, debuggerFun } from '../Utils/Common'

dayjs.locale('zh-cn')

const { hostname, port } = window.location

const Log = lazy(() => import('../Log'))
const Admin = lazy(() => import('../Admin'))
const Start = lazy(() => import(`../Start`))
const Loading = lazy(() => import('../View/Loading/LoadSvg'))
const LocationPage = lazy(() => import(`../View/LocationPage`))
const VersionTips = lazy(() => import('../View/TipsPage/TipsVersion'))

const ProjectRouter = [
    { path: '/', element: <Log /> },
    { path: 'Admin', element: <Admin /> },
    { path: 'Start/:page/:id?', element: <Start /> }
]

const index = () => {
    if (hostname === 'localhost') {
        const [LocaData, setLocaData] = useState([])
        const { send } = useRequest(() => Http({ url: 'getMockProjectList' }), { immediate: false })
        useMount(() => send().then(setLocaData))
        return <LocationPage data={LocaData} />
    }
    else {
        const [Router, setRouter] = useState()
        const [, setUuid] = useSessionStorageState('uuid')
        const [EdiTionState, setEdiTionState] = useState(false)
        const { send } = useRequest(params => Http({ url: 'getProjectConfigInfo', params }), { immediate: false })




        const Routes = ({ data }) => {
            Routes.propTypes = { data: PropTypes.object }
            return useRoutes(data)
        }
        useMount(() => {
            const BrowserVersion = judgeBrowserVersion()
            setEdiTionState(BrowserVersion)
            if (BrowserVersion) {
                send({ webUrl: hostname }).then(async pro => {
                    window.webDB = IndexedDBFun()
                    const { key, config } = pro
                    setUuid(key)
                    const [project] = config
                    const { apiUrl, authUrl, isDebug } = project
                    console.log('是否开启debug', isDebug)
                    window.API = apiUrl
                    window.AUTH = authUrl
                    await AddIndexdDB('project', project)
                    if (!port && isDebug) debuggerFun()
                    setRouter(ProjectRouter)
                })
            }
        })

        return (
            <ConfigProvider locale={zhCN}>
                <App>
                    {Router ?
                        <BrowserRouter>
                            <Suspense fallback={<Loading />} >
                                {EdiTionState ? <Routes data={Router} /> : <VersionTips />}
                            </Suspense>
                        </BrowserRouter> : <Loading />
                    }
                </App>
            </ConfigProvider>
        )
    }
}

export default index